package com.ews.onlineexchange.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.enterprise.context.ConversationScoped;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
import javax.inject.Inject;
import javax.inject.Named;

import com.alibaba.fastjson.JSONObject;
import com.ews.onlineexchange.model.CodeDic;

@Named
@ConversationScoped
public class CodeDicUtils implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	CodeDicFinder codeDicFinder;
	
	private Map<String, List<CodeDic>> codeDicList = new HashMap<String, List<CodeDic>>();
	private Map<String, Map<String, String>> codeDicMap = new HashMap<>();
	
	public List<CodeDic> getCodeDicList(String key) {
		List<CodeDic> list = codeDicList.get(key);
		if (list == null) {
			List<CodeDic> resultList = codeDicFinder.query(key);
			codeDicList.put(key, resultList);
			
			Map<String, String> _map = new HashMap<>();
			for (CodeDic codeDic : resultList) {
				_map.put(codeDic.getCodeValue(), codeDic.getCodeName());
			}
			codeDicMap.put(key, _map);
			return resultList;
		}
		return list;
	}
	
	public Map getCodeDicValueKeyMap(String key) {
		Map<String, String> _map = new HashMap<>();
		List<CodeDic> resultList = codeDicFinder.query(key);
		for (CodeDic codeDic : resultList) {
			_map.put(codeDic.getCodeValue(), codeDic.getCodeName());
		}
		return _map;
	}
	
	
	public Map<String, String> getCodeDicMap(String key) {
		Map<String, String> map = codeDicMap.get(key);
		if (map == null) {
			List<CodeDic> resultList = codeDicFinder.query(key);
			codeDicList.put(key, resultList);
			
			Map<String, String> _map = new HashMap<>();
			for (CodeDic codeDic : resultList) {
				_map.put(codeDic.getCodeValue(), codeDic.getCodeName());
			}
			codeDicMap.put(key, _map);
			return _map;
		}
		return map; 
	}
	
	public Map<String, String> getCodeDicKeyValueMap(String key) {
		Map<String, String> map = codeDicMap.get(key);
		if (map == null) {
			List<CodeDic> resultList = codeDicFinder.query(key);
			codeDicList.put(key, resultList);
			
			Map<String, String> _map = new HashMap<>();
			for (CodeDic codeDic : resultList) {
				_map.put(codeDic.getCodeName(),codeDic.getCodeValue());
			}
			codeDicMap.put(key, _map);
			return _map;
		}
		return map; 
	}
	public String getJsonFromMap(String key) {
		String jsonString = JSONObject.toJSONString(getCodeDicMap(key));
		return jsonString;
	}
	
	public List<SelectItem> getSelectItemsNrfl() {
		List<CodeDic> resultList = getCodeDicList("PetitionType");
		Map<String, SelectItemGroup> groupMap = new TreeMap<String, SelectItemGroup>();
		Map<String, List<SelectItem>> groupSelectItemsMap = new TreeMap<>();
		for (CodeDic codeDic : resultList) {
			String value = codeDic.getCodeValue();
			if (value.endsWith("00")) {
				SelectItemGroup group = new SelectItemGroup(codeDic.getCodeName());
				groupMap.put(codeDic.getCodeValue(), group);
			} else {
				String parentCode = codeDic.getCodeValue().substring(0, 4).concat("00");
				List<SelectItem> selectItemList = groupSelectItemsMap.get(parentCode);
				if (groupSelectItemsMap.get(parentCode) == null) {
					selectItemList = new ArrayList<SelectItem>();
					groupSelectItemsMap.put(parentCode, selectItemList);
				} 
				selectItemList.add(new SelectItem(codeDic.getCodeValue(), codeDic.getCodeName()));
			}
		}
		
		Map<String, List<SelectItemGroup>> map = new TreeMap<>();		
		for (Iterator<String> iterator = groupMap.keySet().iterator(); iterator.hasNext();) {
			String key = iterator.next();
			if (key.endsWith("00") && !key.endsWith("0000")) {
				List<SelectItem> list = groupSelectItemsMap.get(key);
				groupMap.get(key).setSelectItems(list.toArray(new SelectItem[list.size()]));
				
				String parentKey = key.substring(0, 2).concat("0000");
				List<SelectItemGroup> rg = map.get(parentKey);
				if (map.get(parentKey) == null) {
					rg = new ArrayList<SelectItemGroup>();
					map.put(parentKey, rg);
				}
				rg.add(groupMap.get(key));
			}
		}
		
		List<SelectItem> returnList = new ArrayList<SelectItem>();
		for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
			String key = iterator.next();
			List<SelectItemGroup> list = map.get(key);
			groupMap.get(key).setSelectItems(list.toArray(new SelectItem[list.size()]));
			
			returnList.add(groupMap.get(key));
		}
		
		return returnList;
	}
}
